// VirtualWallet.java - 充血模型领域类
package com.elm.domain;

import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@Getter
@Setter
public class VirtualWallet {
    private int id;
    private int userId;
    private Double balance;      // 当前可用余额
    private Double frozen;       // 冻结金额
    private Double overdraft;    // 透支额度
    private Double consumption;  // 累计消费金额
    private Integer vip;         // VIP等级
    private Date createdAt;
    private Date updatedAt;

    // 业务方法 - 充值
    public void recharge(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("充值金额必须大于0");
        }
        this.balance += amount;
        this.updatedAt = new Date();
    }

    // 业务方法 - 提现
    public void withdraw(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("提现金额必须大于0");
        }
        if (this.balance < amount) {
            throw new IllegalStateException("余额不足");
        }
        this.balance -= amount;
        this.updatedAt = new Date();
    }

    // 业务方法 - 支付
    public void pay(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("支付金额必须大于0");
        }

        // 优先使用余额支付
        if (this.balance >= amount) {
            this.balance -= amount;
        }
        // 余额不足时使用透支额度（VIP用户）
        else if (this.overdraft != null && this.overdraft > 0) {
            double remaining = amount - this.balance;
            if (this.overdraft >= remaining) {
                this.balance = 0.0;
                this.overdraft -= remaining;
            } else {
                throw new IllegalStateException("余额和透支额度均不足");
            }
        } else {
            throw new IllegalStateException("余额不足");
        }
        this.updatedAt = new Date();
    }

    // 业务方法 - 冻结金额
    public void freeze(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("冻结金额必须大于0");
        }
        if (this.balance < amount) {
            throw new IllegalStateException("余额不足，无法冻结");
        }
        this.balance -= amount;
        this.frozen += amount;
        this.updatedAt = new Date();
    }

    // 业务方法 - 解冻金额
    public void unfreeze(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("解冻金额必须大于0");
        }
        if (this.frozen < amount) {
            throw new IllegalStateException("冻结金额不足");
        }
        this.frozen -= amount;
        this.balance += amount;
        this.updatedAt = new Date();
    }

    // 业务方法 - 还款
    public void repay(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("还款金额必须大于0");
        }
        this.overdraft -= amount;
        this.updatedAt = new Date();
    }

    // 业务方法 - 增加消费金额并更新VIP等级
    public void addConsumption(Double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("消费金额必须大于0");
        }
        this.consumption += amount;
        updateVipLevel();
        this.updatedAt = new Date();
    }

    // 业务方法 - 更新VIP等级（根据消费金额）
    private void updateVipLevel() {
        if (this.consumption >= 30000) {
            this.vip = 5;
            this.overdraft =  30000.0; // 三级VIP透支额度
        } else if (this.consumption >= 12000) {
            this.vip = 4;
            this.overdraft = 12000.0; // 二级VIP透支额度
        }else if (this.consumption >= 5000) {
            this.vip = 3;
            this.overdraft = 5000.0; // 二级VIP透支额度
        }else if (this.consumption >= 2000) {
            this.vip = 2;
            this.overdraft = 2000.0; // 二级VIP透支额度
        } else if (this.consumption >= 500) {
            this.vip = 1;
            this.overdraft = 500.0; // 一级VIP透支额度
        } else {
            this.vip = 0;
            this.overdraft = 0.0; // 普通用户无透支额度
        }
    }

    // 业务方法 - 检查是否可透支
    public boolean canOverdraft() {
        return this.overdraft != null && this.overdraft > 0;
    }

    // 业务方法 - 获取可用总额（余额 + 透支额度）
    public Double getTotalAvailable() {
        return this.balance + (this.overdraft != null ? this.overdraft : 0.0);
    }
}